<?php
class upload_file {

	/**声明**/

	var $upfile_size, $upfile_name, $upfile, $filename, $type;
	var $up_extention;
	var $prefix, $bad_extention_list, $file_path, $base_directory;
	var $limit_size;

	/**
	 *     构造函数
	 *     @param  $prefix          string 文件名前缀，比如时间，避免上传文件重名
	 *     @param  $bad_extention   string 不允许上传文件扩展名
	 *     @param  $base_directory  string 文件保存目录
	*     @param  $size            int    文件大小
	 *     @return
	 *
	 *     @access public
	 */

	function upload_file($prefix, $bad_extention, $base_directory, $size, $type) {
		$this->bad_extention_list = $bad_extention; //初始化扩展名列表;
		$this->limit_size = $size; //初始化上传文件KB限制;
		$this->prefix = $prefix; //设置文件名称前缀;
		$this->base_directory = $base_directory; //初始化文件上传根目录名，可修改！;
		$this->type = $type;
	}

	/**
	 *     得到$_FILE信息
	*     @param  $upfile_name     string $_FILE[][name]
	*     @param  $upfile_size     string $_FILE[][size]
	 *     @return
	 *
	 *     @access public
	 */

	function set_file_name_size($upfile_name, $upfile_size) {

		$this->upfile_name = $upfile_name; //取得文件名称;
		$this->filename = $this->prefix;
		$this->upfile_size = $upfile_size; //取得文件尺寸;
	}
	/**
	 *     取得文件在服务端储存的临时文件名
	 *     @param  $upfile    string $_FILE[][tmp_name]
	 *     @return
	 *
	 *     @access public
	 */

	function set_upfile($upfile) {
		$this->upfile = $upfile;
	}

	/**
	 *     检测扩展名是否违规
	 *     @return
	 *
	 *     @access private
	 */

	function _check() {

		global $CONF, $ADMIN;
		$check = true;
		$this->up_extention = preg_replace('/.*\.(.*[^\.].*)*/iU', '\\1', $this->upfile_name); //取得文件扩展名;
		$ext = explode("|", $this->bad_extention_list); //以"|"来分解默认扩展名;
		for ($i = 0; each($ext); $i++) //遍历数组;
			{
			if ($ext[$i] == strtolower($this->up_extention)) //比较文件扩展名是否与默认允许的扩展名相符;
				{
				$check = false; //相符则标记;
				break;
			}
		}
		if ($this->type != 'image/pjpeg' AND $this->type != 'image/jpeg' AND $this->type != 'image/gif' AND $this->type != 'application/x-shockwave-flash' AND $this->type != 'image/x-png'){
		    $this->showerror('error1');
			exit;
		}
			
		if ($this->type == 'image/pjpeg' && !$CONF['jpeg_enable']){
		    $this->showerror('error2');
			exit;
		}
		
		if ($this->type == 'image/jpeg' && !$CONF['jpeg_enable']){
		    $this->showerror('error3');
			exit;
		}
			
		if ($this->type == 'image/gif' && !$CONF['gif_enable']){
		    $this->showerror('error4');
			exit;
		}
			
		if ($this->type == 'image/png' && !$CONF['png_enable']){
		    $this->showerror('error5');
			exit;
		}
			

		if (!$check) {
			$this->showmsg('error6' . $this->up_extention);
		}

		/**检测文件大小是否超标**/

		if ($this->upfile_size > round($this->limit_size * 1024)) //文件的大小是否超过了默认的尺寸;

			$this->showerror($ADMIN->lang['undefine_error'] . $this->limit_size . "KB"); //超过则警告;

	}

	/**
	 *     拷贝文件到指定文件
	 *     @return  成功则文件的完整访问路径，失败返回0
	 *
	 *     @access private
	 */

	function _copy_file() {

		global $CONF;

		$b = !file_exists($this->base_directory);
		if ($b) {
			mkdir($this->base_directory, 0777);
		}
		//生成文件完整访问路径;  

		$this->file_path = $this->base_directory . "/" . $this->filename . "." . $this->up_extention;
		$small = $this->base_directory . "/" . $CONF['thumb_prefix'] . $this->filename . "." . $this->up_extention;

		if (copy($this->upfile, $this->file_path)) {
			$this->_resize($small, $this->file_path, "php", "");
			return $this->filename . "." . $this->up_extention; //上传文件;
		} else
			return 0;

		//print $this->showerror("意外错误，请重试！");     //上传失败;

	}
	/**
	 *     完成文件上传
	 *     @return  成功则文件的完整访问路径，失败返回0
	 *
	 *     @access public
	 */

	function do_save() {
		$this->_check(); //检测文件扩展名是否违规,大小是否超限;
		return $this->_copy_file(); //上传文件;
	}

	/** the rest is for debuging **/

	function showerror($errstr = "Undefined error!") {
		echo "<script language=javascript>alert('$errstr');location='javascript:history.go(-1);';</script>";
		exit ();
	}
	function showmsg($msgstr) {
		echo "<script language=javascript>alert('$msgstr');</script>";
	}
	function echo_file_path() {
		return $this->file_path;
	}

	//----------------- Add by 縮圖外掛 ---------------------------
	function _resize($dest, $src, $imglib, $convert_path) {

		$quality = 82; //----JPG

		$image_info = getimagesize($src);
		$srcSize_W = $image_info[0];
		$srcSize_H = $image_info[1];

		$img_aspect = $srcSize_W / $srcSize_H;

		if ($srcSize_H > $srcSize_W * 2)
			define(SMALL_IMAGE_HEIGHT, 100);

		if (SMALL_IMAGE_WIDTH > 0) {
			$destSize_W = SMALL_IMAGE_WIDTH;
			$destSize_H = intval($destSize_W / $img_aspect);
		} else
			if (SMALL_IMAGE_HEIGHT > 0) {
				$destSize_H = SMALL_IMAGE_HEIGHT;
				$destSize_W = intval($destSize_H * $img_aspect);
			} else {
				$destSize_W = 100;
				$destSize_H = intval($destSize_W / $img_aspect);
			}

		if ($imglib == "php") {
			//----------- With buildin php GD function -----------------------          

			$destImage = function_exists('imageCreateTrueColor') ? imageCreateTrueColor($destSize_W, $destSize_H) : imageCreate($destSize_W, $destSize_H);

			//imageAntiAlias($destImage,true);
			//------消鋸齒演算，因看不出效果，故移除以節省時間。

			switch ($image_info[2]) {
				case 1 : //GIF
					//$srcImage = imageCreateFromGif($src); 
					//imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize_W,$destSize_H,$srcSize_W,$srcSize_H);
					// imagegif($destImage,$dest);
					//@copy ($src,$dest);
					break;

				case 2 : //JPEG
					// $srcImage = imageCreateFromJpeg($src);
					//------- 進行縮圖演算---------------
					// imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize_W,$destSize_H,$srcSize_W,$srcSize_H);
					// imageJpeg($destImage,$dest,$quality);
					break;

				case 3 : //PNG
					//$srcImage = imageCreateFromPng($src);
					////------- 進行縮圖演算---------------
					// imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize_W,$destSize_H,$srcSize_W,$srcSize_H);
					// imagePng($destImage,$dest); 
					break;

				default :
					return false;
					break;
			}
			if ($srcImage)
				imagedestroy($srcImage);
			if ($destImage)
				imagedestroy($destImage);

		} else {
			@ copy($src, $dest);
		}

	}
}
?>